// 定义食物类

import Snake from "./Snake";

export default class Food {
  el: HTMLElement = document.querySelector("#food") as HTMLElement;
  constructor() {
    
  }
  get X(): number {
    return this.el.offsetLeft;
  }
  set X(val: number) {
    this.el.style.left = val + "px";
  }
  get Y(): number {
    return this.el.offsetTop;
  }
  set Y(val: number) {
    this.el.style.top = val + "px";
  }

  // 更新食物坐标
  change(snake: Snake): void {
    // 是否找到一个合法的坐标
    let isFind: boolean = false;
    while(! isFind) {
      // 如果蛇已经铺满了屏幕则不在运行
      if (snake.els.length == 30 ** 2) break;
      // 生成随机坐标
      this.X = Math.floor(Math.random() * 30) * 10;
      this.Y = Math.floor(Math.random() * 30) * 10;
      // 随机坐标是否存在蛇的上面
      for(let i of snake.els) {
        if (i.offsetLeft != this.X || i.offsetTop != this.Y) {
          // 坐标合法
          isFind = true;
          break;
        }
      }

    }
  }
}


